package com.hb.trans.global

import android.app.Activity
import android.app.Application
import androidx.fragment.app.Fragment
import com.alibaba.android.arouter.launcher.ARouter
import com.basiclib.utils.L
import com.hb.trans.di.AppComponent
import com.hb.trans.di.DaggerAppComponent
import com.umeng.analytics.AnalyticsConfig
import com.umeng.analytics.MobclickAgent
import com.umeng.commonsdk.UMConfigure
import dagger.android.AndroidInjector
import dagger.android.DispatchingAndroidInjector
import dagger.android.HasActivityInjector
import dagger.android.support.HasSupportFragmentInjector
import javax.inject.Inject


class HbApplication : Application(), HasActivityInjector, HasSupportFragmentInjector {


    @Inject
    lateinit var activityInjector: DispatchingAndroidInjector<Activity>
    @Inject
    lateinit var supportFragmentInject: DispatchingAndroidInjector<androidx.fragment.app.Fragment>
    @Inject
    lateinit var mActivityLifecycleForRxLifecycle: Application.ActivityLifecycleCallbacks

    override fun activityInjector(): AndroidInjector<Activity> = activityInjector

    override fun supportFragmentInjector(): AndroidInjector<androidx.fragment.app.Fragment> = supportFragmentInject

    companion object {
        @JvmStatic
        lateinit var appComponent: AppComponent
        @JvmStatic
        lateinit var appContext: HbApplication
        @JvmStatic
        lateinit var channelName: String
        @JvmStatic
        lateinit var tempString: String
        @JvmStatic
        lateinit var locCity: String//定位市
        @JvmStatic
        lateinit var locProvince: String//定位省
        @JvmStatic
        lateinit var locDistrict: String//定位区县
    }

    override fun onCreate() {
        super.onCreate()
        appContext = this
        daggerInject()
        initLogger()
        initUM()
        initARouter()


    }


    private fun daggerInject() {
        appComponent = DaggerAppComponent
            .builder()
            .application(this)
            .build()
        appComponent.inject(this)
        registerActivityLifecycleCallbacks(mActivityLifecycleForRxLifecycle)
    }

    private fun initUM() {
        UMConfigure.init(this, "5be14925b465f512b2000409", "", UMConfigure.DEVICE_TYPE_PHONE, null)
        UMConfigure.setLogEnabled(!IS_RELEASE)
        channelName = AnalyticsConfig.getChannel(this)
        MobclickAgent.openActivityDurationTrack(false)
        L.d(msg = " channel name = $channelName")
    }

    fun initLogger() {
        L("cqt_", IS_RELEASE)
    }

    fun initARouter() {
        if (!IS_RELEASE) {
            ARouter.openLog()    // 打印日志
            ARouter.openDebug()   // 开启调试模式(如果在InstantRun模式下运行，必须开启调试模式！线上版本需要关闭,否则有安全风险)
        }
        ARouter.init(this) // 尽可能早，推荐在Application中初始化
    }

}